# -*- coding: utf-8 -*-

import os
import sys
from PyQt4.QtCore import QCoreApplication

plat = sys.platform.lower()
iswindows = 'win32' in plat or 'win64' in plat
isosx = 'darwin' in plat
islinux = not(iswindows or isosx or isbsd)
isxp = iswindows and sys.getwindowsversion().major < 6
is64bit = sys.maxsize > (1 << 32)
isfrozen  = hasattr(sys, 'frozen')


appSettings = None


# Translation macro
def tr(msg):
    return QCoreApplication.translate("@default", msg)

MultiLanguage = {}
MultiLanguage['account_en'] = tr('English')
MultiLanguage['account_cn'] = tr('Chinese')

try:
    if iswindows:
        # HOMEPATH = os.environ["APPDATA"]
        HOMEPATH = os.path.expandvars(u'%APPDATA%')
        HOMEDOC = os.path.expandvars(u'%USERPROFILE%')
    else:
        HOMEDOC = HOMEPATH = os.path.expanduser(u'~')
except Exception as e:
    print 'Falied with get home path', e
    HOMEPATH = os.path.join('./')


STYLE_SCROLLBAR = "QScrollBar{background:transparent; width: 10px;}" \
                                          "QScrollBar::handle{background:lightgray; border:2px solid transparent; border-radius:5px;}" \
                                          "QScrollBar::handle:hover{background:gray;}" \
                                          "QScrollBar::sub-line{background:transparent;}" \
                                          "QScrollBar::add-line{background:transparent;}"


class CAppSettings:
    dictSettings = {}

    def __init__(self, appName):
        self.initSettings = {}
        self.initSettings['version'] = res.version
        self.initSettings['dbVersion'] = res.dbVersion
        self.initSettings['language'] = 'cn'

        self.dictSettings = self.loadSettings()
        self.saveSettings()

    def loadSettings(self):
        if os.path.exists(os.path.join(res.userPath, 'config')):
            self.initSettings = eval(open(os.path.join(res.userPath, 'config'), 'rb').read())
            for i in self.initSettings.keys():
                if not self.dictSettings.__contains__(i):
                    self.dictSettings[i] = self.initSettings[i]
        else:
            self.dictSettings = self.initSettings

        return self.dictSettings

    def saveSettings(self):
        with open(os.path.join(res.userPath, 'config'), 'w') as w:
            w.write(str(self.dictSettings));


def initSettings(appName):
    global appSettings
    appSettings = CAppSettings(appName)


"""
Product class
"""
class AccountBook(object):
    version = '1.0.0.1'
    dbVersion = '1.0'
    title = 'Account Book v%s' % version

    def __init__(self):
        if not os.path.exists(self.userPath):
            os.makedirs(self.userPath)

    @property
    def userPath(self):
        return os.path.join(HOMEPATH, '.' + self.__class__.__name__)


res = AccountBook()
